package jcu.sal.client;

import java.rmi.RemoteException;

import jcu.sal.common.agents.SALAgent;
import jcu.sal.common.agents.SALAgentFactory;
import jcu.sal.common.exceptions.ConfigurationException;

/**
 * @author Marc Hammerton
 * 
 */
public class SALClientStart {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ConfigurationException {
		SALClient client = null;
		SALAgent agent = null;

		if (args[0].equals("RMI")) {
			try {
				agent = SALAgentFactory.getFactory().createRMIAgent(args[1],
						args[2], args[3]);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} else if (args[0].equals("NETTY")) {
			agent = SALAgentFactory.getFactory().createNettyAgent(args[1], args[2]);
		}

		try {
			client = new SALClient(agent);
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}

		client.start();
		try {
			client.run();
		} finally {
			try {
				client.stop();
			} catch (Throwable e) {
			}
		}

		if (args[0].equals("RMI")) {
			SALAgentFactory.getFactory().releaseRMIAgent(agent);
		} else if (args[0].equals("NETTY")) {
			SALAgentFactory.getFactory().releaseNettyAgent(agent);
		}

		System.out.println("Main exiting");
		// required for RMI client
		System.exit(0);
	}

}
